package org.summer.lawyer.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.summer.common.core.domain.R;
import org.summer.common.core.validate.AddGroup;
import org.summer.common.core.validate.EditGroup;
import org.summer.common.excel.utils.ExcelUtil;
import org.summer.common.idempotent.annotation.RepeatSubmit;
import org.summer.common.log.annotation.Log;
import org.summer.common.log.enums.BusinessType;
import org.summer.common.mybatis.core.page.PageQuery;
import org.summer.common.mybatis.core.page.TableDataInfo;
import org.summer.common.web.core.BaseController;
import org.summer.lawyer.domain.bo.paytransfer.PayTransferBo;
import org.summer.lawyer.domain.bo.userwithdraw.UserWithdrawBo;
import org.summer.lawyer.domain.bo.userwithdraw.WithdrawQrcodeBo;
import org.summer.lawyer.domain.vo.userwithdraw.UserWithdrawVo;
import org.summer.lawyer.domain.vo.userwithdraw.WithdrawQrcodeVo;
import org.summer.lawyer.service.IPayTransferService;
import org.summer.lawyer.service.IUserWithdrawService;
import org.summer.lawyer.util.IdWorkerUtil;

import java.util.List;

/**
 * 用户提现记录
 *
 * @author summer
 * @date 2024-06-25
 */
@Slf4j
@Tag(name = "后台相关接口", description = "用户提现记录")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/lawyer/userWithdraw")
public class UserWithdrawController extends BaseController {

    private final IUserWithdrawService userWithdrawService;
    private final IPayTransferService payTransferService;


    /**
     * 查询用户提现记录列表
     */
    @Operation(summary = "查询用户提现记录列表接口", description = "查询用户提现记录列表", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("pay:withdraw:list")
    @GetMapping("/list")
    public TableDataInfo<UserWithdrawVo> list(UserWithdrawBo bo, PageQuery pageQuery) {
        for(int i=0; i < 100; i++){
            log.info(IdWorkerUtil.nextIdStr());
        }
        return userWithdrawService.selectPageList(bo, pageQuery);
    }

    /**
     * 导出用户提现记录列表
     */
    @Operation(summary = "导出用户提现记录列表接口", description = "导出用户提现记录列表")
    @SaCheckPermission("pay:withdraw:export")
    @Log(title = "用户提现记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(UserWithdrawBo bo, HttpServletResponse response) {
        List<UserWithdrawVo> list = userWithdrawService.selectUserWithdrawVoList(bo);
        ExcelUtil.exportExcel(list, "用户提现记录", UserWithdrawVo.class, response);
    }

    /**
     * 获取用户提现记录详细信息
     *
     * @param id 主键
     */
    @Operation(summary = "获取用户提现记录详细信息接口", description = "查询用户提现记录列表", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("pay:withdraw:query")
    @GetMapping("/{id}")
    public R<UserWithdrawVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(userWithdrawService.selectUserWithdrawVoById(id));
    }

    /**
     * 新增用户提现记录
     */
    @Operation(summary = "新增用户提现记录接口", description = "新增用户提现记录", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("pay:withdraw:add")
    @Log(title = "用户提现记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/saveUserWithdraw")
    public R<Void> saveUserWithdraw(@Validated(AddGroup.class) @RequestBody UserWithdrawBo bo) {
        userWithdrawService.applyWithdraw(bo);
        return R.ok();
    }

    /**
     * 修改用户提现记录
     */
    @Operation(summary = "修改用户提现记录接口", description = "修改用户提现记录", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("pay:withdraw:edit")
    @Log(title = "用户提现记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/updateUserWithdraw")
    public R<Void> updateUserWithdraw(@Validated(EditGroup.class) @RequestBody UserWithdrawBo bo) {
        return toAjax(userWithdrawService.updateByBo(bo));
    }

    /**
     * 删除用户提现记录
     *
     * @param ids 主键串
     */
    @Operation(summary = "删除用户提现记录接口", description = "删除用户提现记录", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("pay:withdraw:remove")
    @Log(title = "用户提现记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeUserWithdraw/{ids}")
    public R<Void> removeUserWithdraw(@NotEmpty(message = "主键不能为空")
                                      @PathVariable Long[] ids) {
        return toAjax(userWithdrawService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 提现申请审批驳回
     */
    @SaCheckPermission("pay:withdraw:reject")
    @Log(title = "提现申请审批驳回", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PostMapping("/rejectWithdraw")
    public R<Void> rejectWithdraw(@Validated @RequestBody UserWithdrawBo bo){
        userWithdrawService.rejectWithdraw(bo);
        return R.ok();
    }

    /**
     * 提现
     */
    @SaCheckPermission("pay:withdraw:submit")
    @Log(title = "提现审核通过", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PostMapping("/submitWithdraw")
    public R<Void> submitWithdraw(@Validated @RequestBody PayTransferBo bo){
        payTransferService.submitTransferOrder(bo);
        return R.ok();
    }

    /**
     * 获取提现二维码信息
     */
    @SaIgnore
    @GetMapping("/getWithdrawQrcode")
    public R<WithdrawQrcodeVo> getWithdrawQrcode(WithdrawQrcodeBo bo){
        return R.ok(userWithdrawService.selectWithdrawQrcodeVoById(bo));
    }

    @Operation(summary = "更新二维码图片")
    @SaCheckPermission("pay:withdraw:qrcode")
    @PostMapping("/updateWithdrawQrcode")
    public R<Void> updateWithdrawQrcode(@Validated @RequestBody WithdrawQrcodeBo bo){
        userWithdrawService.updateWithdrawQrCodeImage(bo);
        return R.ok();
    }
}
